package com.lykj.llw.box.ui.diafrg.agent

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.lifecycle.lifecycleScope
import com.lykj.lib_base.app.BaseApplication
import com.lykj.lib_base.base.BaseDiaFrg
import com.lykj.lib_base.bus.FlowBus
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.llw.box.databinding.DiafrgAgentChooseBinding
import com.lykj.llw.box.ui.diafrg.ShareDiaFrg

/**
 * ================================================
 * 代理选择新用户/旧用户
 * ================================================
 *
 * @author gosha
 * @date 2023/12/4-16:32
 */
class AgentChooseDiaFrg : BaseDiaFrg() {
    lateinit var binding: DiafrgAgentChooseBinding

    private var mCouponId = 0
    private var mCouponMoney: Double = 0.0

    override fun onStart() {
        super.onStart()

        val window = dialog!!.window
        //设置窗口弹出动画
        window!!.setWindowAnimations(0)
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val layoutParams = window.attributes
        layoutParams.apply {
            dimAmount = 0.5f
            gravity = Gravity.CENTER
            width = WindowManager.LayoutParams.MATCH_PARENT
            height = WindowManager.LayoutParams.WRAP_CONTENT
        }

        if (null != dialog) {
            val dm = DisplayMetrics()
            requireActivity().windowManager.defaultDisplay.getMetrics(dm)
            dialog?.apply {
                setCanceledOnTouchOutside(true)
                window.setGravity(Gravity.CENTER)
            }
        }
    }

    open fun setCouponId(id: Int): AgentChooseDiaFrg {
        this.mCouponId = id
        return this
    }

    open fun setCouponMoney(money: Double): AgentChooseDiaFrg {
        mCouponMoney = money
        return this
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DiafrgAgentChooseBinding.inflate(layoutInflater)
        initUI()
        initData()
        setListener()
        return binding.root
    }

    override fun initUI() {
        FlowBus.with<String>(BConstants.BUS_KEY.GIFT_SUCCESS).register(this) {
            FlowBus.with<String>("refresh_voucher")
                .post(requireActivity().lifecycleScope, "")

            dismiss()
        }
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.tvNewUser.clickWithTrigger {
            ShareDiaFrg()
                .setShareParam(
                    "盒子",
                    "攒趣盒子是安全专业的手游平台，集精品游戏、福利游戏、BT游戏于一体的精品手机游戏平台",
                    "",
                    "http://hezi-h5.66wsy.com/hezi_coupon?param=${BaseApplication.instance?.getAgentUserInfo()?.id}-${mCouponId}&couponPrice=${mCouponMoney.toInt()}"
                )
                .show(childFragmentManager, "分享弹窗")
        }

        binding.tvOldUser.clickWithTrigger {
            AgentVoucherGiftDiaFrg()
                .setCouponId(mCouponId)
                .show(childFragmentManager, "赠送弹窗")
        }
    }
}